package com.iwomy.envpro.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iwomy.envpro.platform.VO.AirSiteVO;
import com.iwomy.envpro.platform.entity.AirSiteEntity;
import com.iwomy.envpro.platform.entity.CodeAreaEntity;
import com.iwomy.envpro.platform.mapper.AirSiteMapper;
import com.iwomy.envpro.platform.service.AirSiteService;
import com.iwomy.envpro.platform.service.CodeAreaService;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 监测点
 *
 * @author pig
 * @date 2023-08-26 14:22:49
 */
@Service
public class AirSiteServiceImpl extends ServiceImpl<AirSiteMapper, AirSiteEntity> implements AirSiteService {

	@Resource
	private CodeAreaService codeAreaService;

	@Override
	public Page getAirSitePage(Page page,Long id) {
		List<CodeAreaEntity> codeAreaEntities = codeAreaService.recursionAll(id);
		List<Long> collect = codeAreaEntities.stream().map(CodeAreaEntity::getId).collect(Collectors.toList());
		AirSiteEntity airSiteEntity = this.getById(id);
		LambdaQueryWrapper<AirSiteEntity> queryWrapper = Wrappers.<AirSiteEntity>lambdaQuery();


		if(ObjectUtil.isNotEmpty(collect)){
			queryWrapper.in(AirSiteEntity::getAreaId, collect);
		}
		if(ObjectUtil.isNotNull(airSiteEntity)){
			queryWrapper.or().eq(AirSiteEntity::getId,id);
		}
		if(ObjectUtil.isEmpty(collect)&&ObjectUtil.isEmpty(airSiteEntity))return page;

		return this.page(page,queryWrapper);
	}
	@Override
	public List<AirSiteEntity> getAirSiteList(Long id) {

		AirSiteEntity airSiteEntity = getById(id);
		if(ObjectUtil.isNotEmpty(airSiteEntity)){
			ArrayList<AirSiteEntity> airSiteEntities = new ArrayList<>();
			airSiteEntities.add(airSiteEntity);
			return airSiteEntities;
		}


		List<CodeAreaEntity> codeAreaEntities = codeAreaService.recursionAll(id);
		List<Long> collect = codeAreaEntities.stream().map(CodeAreaEntity::getId).collect(Collectors.toList());

		if(ObjectUtil.isEmpty(collect)){
			return new ArrayList<>();
		}

		LambdaQueryWrapper<AirSiteEntity> queryWrapper = Wrappers.<AirSiteEntity>lambdaQuery();

		queryWrapper.in(AirSiteEntity::getAreaId, collect);
		return this.list(queryWrapper);
	}

	@Override
	public List<AirSiteVO> getAirSiteListAll(CodeAreaEntity codeAreaEntity) {
		List<CodeAreaEntity> list = codeAreaService.list(Wrappers.lambdaQuery(codeAreaEntity));
		List<AirSiteVO> airSiteVOS = BeanUtil.copyToList(list, AirSiteVO.class);
		List<AirSiteEntity> airSiteEntities = this.getBaseMapper().selectList(Wrappers.lambdaQuery());
		airSiteEntities.stream().forEach(x->{
			AirSiteVO airSiteVO = BeanUtil.copyProperties(x, AirSiteVO.class);
			//用于判断是否能添加公司
			airSiteVO.setLevel(-1);
			airSiteVO.setMonitor(x.getMonitorTypeName());
			airSiteVO.setPid(x.getAreaId());
			airSiteVOS.add(airSiteVO);
		});
		return airSiteVOS;
	}

	/**
	 * APP只要 区、镇、检测点
	 * @return
	 */
	@Override
	public List<AirSiteVO> appAirSitePage() {
		LambdaQueryWrapper<CodeAreaEntity> queryWrapper = Wrappers.lambdaQuery();
		List<Integer> ints= new ArrayList();
		ints.add(3);
		ints.add(4);
		queryWrapper.in(CodeAreaEntity::getLevel,ints);
		List<CodeAreaEntity> list3 = codeAreaService.list(queryWrapper);
		List<AirSiteVO> airSiteVOS = BeanUtil.copyToList(list3, AirSiteVO.class);
		List<AirSiteEntity> airSiteEntities = this.getBaseMapper().selectList(Wrappers.lambdaQuery());
		airSiteEntities.stream().forEach(x->{
			AirSiteVO airSiteVO = BeanUtil.copyProperties(x, AirSiteVO.class);
			airSiteVO.setLevel(5);
			airSiteVO.setMonitor(x.getMonitorTypeName());
			airSiteVO.setPid(x.getAreaId());
			airSiteVOS.add(airSiteVO);
		});
		return airSiteVOS;
	}
}